package com.guli.eduService.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guli.commonUtils.R;
import com.guli.eduService.entity.EduTeacher;
import com.guli.eduService.entity.vo.TeacherQuery;
import com.guli.eduService.service.EduTeacherService;
import com.guli.serviceBase.Entity.GuliExcepiton;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author zxc
 * @since 2023-03-26
 */
@Api(description = "讲师管理模块")
@RestController
@RequestMapping("/eduService/teacher")
//@CrossOrigin // 解决跨域问题
public class EduTeacherController {
    @Autowired
    private EduTeacherService teacherService;

    @ApiOperation(value = "所有讲师列表")
    @GetMapping("/findAll")
    public R listAllTeachers(){
        List<EduTeacher> result = teacherService.list(null);
        return R.ok().data("Teachers" , result);
    }

    @ApiOperation(value = "逻辑删除讲师")
    @DeleteMapping("/remove/{id}")
    public R removeTeacher(@ApiParam(name = "id" , value = "讲师ID" , required = true)
                                     @PathVariable String id){
        boolean flag = teacherService.removeById(id);
        if(flag){
            return R.ok();
        }
        else {
            return R.error();
        }
    }

    @ApiOperation(value = "分页讲师列表")
    @GetMapping("pageTeacher/{page}/{limit}")
    public R pageList(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit){
        // 创建page对象
        Page<EduTeacher> pageParam = new Page<>(page, limit);
        // 调用方法，来实现分页
        // 调用方法时，底层封装，把分页的所有数据全部封装到pageParam对象中;
        teacherService.page(pageParam, null);
        // 获取当前指定页面的记录
        List<EduTeacher> records = pageParam.getRecords();
        long total = pageParam.getTotal(); // 总记录数

        Map<String , Object> hashMap = new HashMap<>();
        hashMap.put("toal" , total);
        hashMap.put("row" , records);
        return R.ok().data(hashMap);
//        return  R.ok().data("total", total).data("rows", records);
    }

    // 条件查询 && 带分页功能
    @ApiOperation(value = "带有查询条件的分页讲师列表")
    @PostMapping("/pageListCondition/{page}/{limitRecords}")
    public R pageListCondition(
            @ApiParam(name = "page" , value = "当前页码" ,required = true)
            @PathVariable Long page,

            @ApiParam(name = "limitRecords" , value = "每页记录数" , required = true)
            @PathVariable Long limitRecords,

            @ApiParam(name = "teacherQuery" , value = "讲师查询条件" ,required = true)
            @RequestBody TeacherQuery teacherQuery
            // 即，使用JSON形式来传递数据，同时将JSON数据封装到指定对象teacherQuery中;
            ){
        Page<EduTeacher> eduTeacherPage = new Page<>(page , limitRecords);
        // 调用service层中，自定义封装的条件查询 && 分页;
        teacherService.QueryThroughConditions(eduTeacherPage , teacherQuery);
        List<EduTeacher> records = eduTeacherPage.getRecords();
        long total = eduTeacherPage.getTotal();
        Map<String , Object> map = new HashMap<>();
        map.put("total" , total);
        map.put("records" , records);
        return R.ok().data(map);
    }

    // 添加讲师模块
    @ApiOperation(value = "添加讲师模块")
    @PostMapping("/addteacher")
    public R addTeacher(
            @ApiParam(name = "eduTeacher" , value = "所要添加的讲师" ,required = true)
            @RequestBody EduTeacher eduTeacher){
        boolean flag = teacherService.save(eduTeacher);
        if(flag){
            return R.ok();
        }
        return R.error();
    }

    // 根据id，对于讲师进行查询的模块
    @ApiOperation(value = "根据ID查询讲师模块")
    @GetMapping("/Queryteacher/{id}")
    public R addTeacher(
            @ApiParam(name = "id" , value = "所要查询的讲师ID" ,required = true)
            @PathVariable String id){
        EduTeacher teacher = teacherService.queryById(id);
        // 测试异常/自定义异常Exception
//        try{
//            int i = 10 / 0;
//        }
//        catch(Exception e){
//            throw new GuliExcepiton(2001 , "出现自定义异常");
//        }
        if(teacher !=null){
            return R.ok().data("teacher" , teacher);
        }
        return R.error().data("result" , "该讲师不存在!!!");
    }

    // 根据ID，对于讲师进行修改的模块
    @ApiOperation(value = "根据ID对于讲师进行修改模块")
    @PutMapping("/Updateteacher/{id}")
    public R updateTeacher(
            @ApiParam(name = "id" , value = "所要查询的讲师ID" ,required = true)
            @PathVariable String id,

            @ApiParam(name = "eduTeacher" , value = "所要修改的讲师" ,required = true)
            @RequestBody EduTeacher eduTeacher){
        if(teacherService.getById(id) == null){
            return R.error().data("teacher" , "该讲师不存在！！！");
        }
        eduTeacher.setId(id);
        boolean flag = teacherService.updateById(eduTeacher);
        if(!flag){
            return R.error().data("result" , "修改失败");
        }
        return R.ok().data("result" , "修改成功");
    }
    // 根据ID，对于讲师进行修改的模块
    @ApiOperation(value = "根据ID对于讲师进行修改模块")
    @PostMapping("/Updateteacher")
    public R updateTeacher(
            @ApiParam(name = "eduTeacher" , value = "所要修改的讲师" ,required = true)
            @RequestBody EduTeacher eduTeacher){
        // 前端页面所传递过来的所需要修改对象信息中，指定所需要修改对象的id;
        boolean flag = teacherService.updateById(eduTeacher);
        if(!flag){
            return R.error().data("result" , "修改失败");
        }
        return R.ok().data("result" , "修改成功");
    }
}

